<template>
  <div class="container">
    <div class="header">
      <div class="title">部门列表</div>
      <el-button
        style="margin-left: 30px"
        @click.prevent="handleAdd"
        type="primary"
        plain
        size="medium"
        v-permission="{ permission: '创建部门', type: 'disabled' }"
        >创建部门</el-button
      >
      <div style="margin-left: 30px">
        <lin-search @query="onQueryChange" ref="searchKeyword" />
      </div>
    </div>
    <!-- <transition name="fade">
        <div class="search" v-if="keyword">
          <p class="search-tip">
            搜索“<span class="search-keyword">{{ keyword }}</span
            >”， 找到 <span class="search-num">{{ totalNums }}-{{currentPage}}</span> 条信息
          </p>
          <button class="search-back" @click="backInit">返回</button>
        </div>
    </transition> -->
    <el-table stripe v-loading="loading" :data="tableData">
      <el-table-column fixed="left" prop="dwbhzdy" label="编号" width="150"></el-table-column>
      <el-table-column
        fixed="left"
        prop="dwmc"
        label="名称"
        width="500"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column fixed="left" prop="dwdh" label="电话" width="200"></el-table-column>
      <el-table-column prop="dwbz" label="备注" min-width="800" :show-overflow-tooltip="true"></el-table-column>
      <!-- <el-table-column prop="dwyxf" label="有效否" width="100">
        <template slot-scope="scope"><p style="color: red;">{{ scope.row.dwyxf | dwyxfFormat }}</p></template>
      </el-table-column> -->
      <el-table-column fixed="right" width="220" label="操作">
        <template slot-scope="scope">
          <el-button @click.prevent="handleSubList(scope.row)" type="primary" plain size="mini">子部门</el-button>
          <el-button @click.prevent="handleEdit(scope.row)" type="primary" plain size="mini">编辑</el-button>
          <el-button
            @click.prevent="handleDelete(scope.row)"
            type="danger"
            plain
            size="mini"
            v-permission="{ permission: '删除部门', type: 'disabled' }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :background="true"
        :page-size="pageCount"
        :current-page="currentPage"
        v-if="refreshPagination"
        layout="prev, pager, next, jumper"
        :total="totalNums"
      ></el-pagination>
    </div>
    <!--  创建/编辑部门弹窗  -->
    <department-edit
      v-if="dialogFormVisible"
      :isCreate="isCreate"
      :dialogFormVisible="dialogFormVisible"
      :departmentId="departmentId"
      @dialogClose="dialogClose"
    ></department-edit>
  </div>
</template>

<script>
import LinSearch from '@/component/base/search/lin-search'
import department from '@/model/department'
import departmentEdit from './department-edit'

export default {
  components: {
    departmentEdit,
    LinSearch,
  },
  data() {
    return {
      display: true,
      tableData: [],
      departmentId: 0,
      totalkey: {},
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      refreshPagination: true,
      dialogFormVisible: false, // 展示弹窗
      isCreate: false,
      keyword: null,
      searchKeyword: '',
      root: 1, // 是否是父部门,
      form: {
        name: '',
        is_root: 0,
        parent_id: null,
        dwbhzdy: '',
        dwmc: '',
        dwdh: '',
        dwbz: '',
        dwyxf: 1,
      },
    }
  },
  async created() {
    this.loading = true
    this.getCategories()
    this.loading = false
  },
  filters: {
    dwyxfFormat(val) {
      return val === 1 ? '√' : ''
    },
  },
  watch: {
    display(val) {
      this.form.dwyxf = val ? 1 : 0
    },
    // 关键字搜索
    searchKeyword(newVal) {
      if (newVal) {
        this.keyword = newVal
        if (this.value == null) {
          this.StartTime = ''
          this.EndTime = ''
        } else {
          this.StartTime = this.value[0]
          this.EndTime = this.value[1]
        }
        if (this.searchUser) {
          this.keyword = `${newVal}`
        }
      } else {
        this.keyword = ''
        this.$refs.searchKeyword.clear()
      }
      this.searchPage()
    },
  },
  methods: {
    async getCategories() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const categories = await department.getCategories(page, count, this.root)
      this.tableData = categories.items
      this.totalNums = categories.total
    },
    async searchPage() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const categories = await department.search({
        keyword: this.searchKeyword,
        page,
        count,
      })
      this.tableData = categories.items
      this.totalNums = categories.total
    },
    // 切换table页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this.getCategories()
      this.loading = false
    },
    // 获取所拥有的权限并渲染  由子组件提供
    handleAdd() {
      this.dialogFormVisible = true
      this.isCreate = true
    },
    handleEdit(val) {
      this.departmentId = val.id
      this.isCreate = false
      this.dialogFormVisible = true
    },
    handleDelete(val) {
      let res
      this.$confirm('此操作将永久删除该部门，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        try {
          this.loading = true
          res = await department.deletedepartment(val.id)
        } catch (e) {
          this.loading = false
          console.error(e)
        }
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.loading = false
          if (this.totalNums % this.pageCount === 1 && this.currentPage !== 1) {
            this.currentPage--
          }
          this.getCategories()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        } else {
          this.loading = false
          this.$message.error(`${res.message}`)
        }
      })
    },
    handleSubList(row) {
      this.$router.push({
        path: `/sub-department/${row.id}/list`,
        query: { departmentName: row.name },
      })
    },
    dialogClose() {
      this.dialogFormVisible = false
      this.loading = true
      this.getCategories()
      this.loading = false
    },
    // 清空检索
    async backInit() {
      this.searchKeyword = ''
      this.keyword = ''
      this.totalkey = {}
      this.loading = true
      this.loading = false
    },
    async onQueryChange(query) {
      this.searchKeyword = query.trim()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .search {
    height: 52px;
    width: 100%;
    background: rgba(57, 99, 188, 0.1);
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 24px;
    margin-bottom: 24px;

    .search-tip {
      margin-left: 40px;
      height: 52px;
      line-height: 52px;
      color: #354058;
      font-size: 14px;

      .search-keyword {
        color: $theme;
      }

      .search-num {
        color: #f4516c;
      }
    }

    .search-back {
      margin: 8px 20px;
      height: 32px;
      background: #f4516c;
      border: none;
      border-radius: 2px;
      color: #fff;
      padding: 0 13px;
      font-size: 14px;
      cursor: pointer;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
